/**
 * Created by jf on 2015/9/11.
 */

$(function () {

    var pageManager = {
        $container: $('.js_container'),
        _pageStack: [],
        _configs: [],
        _defaultPage: null,
        _isGo: false,
        default1: function (defaultPage) {
            this._defaultPage = defaultPage;
            return this;
        },
        init: function () {
            var self = this;

            $(window).on('hashchange', function (e) {

                console.log(self._isGo);

                var _isBack = !self._isGo;
                self._isGo = false;
                if (!_isBack) {
                    return;
                }

                var url = location.hash.indexOf('#') === 0 ? location.hash : '#';
                var found = null;
                for(var i = 0, len = self._pageStack.length; i < len; i++){
                    var stack = self._pageStack[i];
                    if (stack.config.url === url) {
                        found = stack;
                        break;
                    }
                }
                if (found) {
                    self.back();
                }
                else {
                    goDefault();
                }
            });

            function goDefault(){
                var url = location.hash.indexOf('#') === 0 ? location.hash : '#';
                var page = self._find('url', url) || self._find('name', self._defaultPage);
                self.go(page.name);
            }

            goDefault();

            return this;
        },
        push: function (config) {
            this._configs.push(config);
            return this;
        },
        go: function (to) {
            var config = this._find('name', to);
            if (!config) {
                return;
            }

            var html = $(config.template).html();
            if(config.name=="home"){
            	var $html = $(html).addClass(config.name);
            }
            else{
            	var $html = $(html).addClass('slideIn').addClass(config.name);
            }           
            this.$container.append($html);
            this._pageStack.push({
                config: config,
                dom: $html
            });
            this._isGo = true;
            location.hash = config.url;

            if (!config.isBind) {
                this._bind(config);
            }
            if(config.name=="button1"){
            	var height_screen=document.documentElement.clientHeight-45;
            	$(".main-info").height(height_screen);
            	refresher.init({
            		id:"wrapper",//<------------------------------------------------------------------------------------┐
            		pullDownAction:Refresh,                                                            
            		pullUpAction:Load 																			
            		});	
            	var generatedCount = 0;	
            	$.ajax({
            		type: "POST",
            		url: "/SmartCommunity/wechat/get_notices_list",
            		contentType: "application/json; charset=utf-8",
            		success: function(data){		
            			//var arr=eval("["+data+"]");
            			var arr=eval("("+data+")");
            			for(var i=0;i<arr.length;i++)
            			{
            				var row = $(".info-template").eq(0).clone();
            				row.attr("id",arr[i].id);
            				row.find(".time").text(arr[i].createTime);
            				row.find(".sum").text(arr[i].createTime);
            				row.find(".status").text(arr[i].createTime);
            				row.css("display","block");
            				row.appendTo("#info-datas");
            				 wrapper.refresh();
            			}
            		},
            		error: function(){
            		alert("网络请求失败");
            		} 
            		}); 																
            function Refresh() {																
            	 var el,row;																		
            		el =document.querySelector("#wrapper ul");	
            		$.ajax({
            			type: "POST",
            			url: "/SmartCommunity/wechat/get_notices_list",
            			contentType: "application/json; charset=utf-8", 
            			success: function(data){
            				var arr=eval(data);
            				for(var i=0;i<arr.length;i++)
            				{
            					var row = $(".info-template").eq(0).clone();
            					row.attr("id",arr[i].id);
            					row.find("div").text(arr[i].createTime);
            					row.find("h2").text(arr[i].title);
            					row.find("p").text(arr[i].summary);
            					row.css("display","block");
            					if(i==0){
            					el.innerHTML='';}
            					row.appendTo("#info-datas");
            				}
            				 setTimeout(function(){
            	                    wrapper.refresh();
            	                },1000);
            			},
            			error: function(){
            			alert("网络请求失败");
            			} 
            			}); 		
            }

            function Load() {
            	var temp=$("ul li:last-child").attr("id");
            	$.ajax({
            		type: "POST",
            		url: "/SmartCommunity/wechat/get_notices_list?infoid="+temp,
            		contentType: "application/json; charset=utf-8",
            		success: function(data){
            			var arr=eval(data);
            			for(var i=0;i<arr.length;i++)
            			{
            				var row = $(".info-template").eq(0).clone();
            				row.attr("id",arr[i].id);
            				row.find("div").text(arr[i].createTime);
            				row.find("h2").text(arr[i].title);
            				row.find("p").text(arr[i].summary);
            				row.css("display","block");
            				row.appendTo("#info-datas");
            			}
            			 setTimeout(function(){
                             wrapper.refresh();
                         },1000);
            		},
            		error: function(){
            		alert("网络请求失败");
            		} 
            		}); 		
            }
            }
            else if(config.name=="home"){
            	$.ajax({
            		type: "POST",
            		url: "/SmartCommunity/wechat/get_user_info",
            		contentType: "application/json; charset=utf-8",
            		success: function(data){
            			alert(data);
            			var arr=eval("("+data+")");
            			$(".touxiang").attr("src",arr.headimgurl);
            			$(".name").text(arr.nickname);
            		},
            		error: function(){
            		alert("网络请求失败");
            		} 
            		});
            }
            else if(config.name=="button"){
            	$.ajax({
            		type: "POST",
            		url: "/SmartCommunity/wechat/get_user_info",
            		contentType: "application/json; charset=utf-8",
            		success: function(data){
            			alert(data);
            			var arr=eval("("+data+")");
            			$(".touxiang").attr("src",arr.headimgurl);
            			$(".name").text(arr.nickname);
            		},
            		error: function(){
            		alert("网络请求失败");
            		} 
            		});
            }
            return this;
        },
        back: function () {
            var stack = this._pageStack.pop();
            if (!stack) {
                return;
            }
            stack.dom.addClass('slideOut').on('animationend', function () {
                stack.dom.remove();
            }).on('webkitAnimationEnd', function () {
                stack.dom.remove();
            });

            return this;
        },
        _find: function (key, value) {
            var page = null;
            for (var i = 0, len = this._configs.length; i < len; i++) {
                if (this._configs[i][key] === value) {
                    page = this._configs[i];
                    break;
                }
            }
            return page;
        },
        _bind: function (page) {
            var events = page.events || {};
            for (var t in events) {
                for (var type in events[t]) {             
                    this.$container.on(type, t, events[t][type]);
                }
            }
            page.isBind = true;
        }
    };

    var home = {
        name: 'home',
		url:"#",
		template: '#tpl_home',
        events: {
            '.js_cell': {
                click: function (e) {
                    var id = $(this).data('id');		
                    pageManager.go(id);
                }
            }
        }
    };
    var button = {
        name: 'button',
        url: '#button',
        template: '#tpl_button',
        events: {
        	'#showTooltips': {
                click: function () {
                    	 var temp=$(".money").val();
                         if(temp==""){
                        	 var $tooltips = $('.js_tooltips');
                        	 $tooltips.show();
                             setTimeout(function () {
                                 $tooltips.hide();
                             }, 2000);
                             $(".money").focus();
                             return false;
                         }
                         $.ajax({
                     		type: "POST",
                     		url: "",
                     		contentType: "application/json; charset=utf-8",
                     		success: function(data){
                     			
                     		},
                     		error:function(){
                     			alert("error");
                     			}
                     		});
                     }
                 }
             }
    };
    var button1 = {
            name: 'button1',
            url: '#button1',
            template: '#tpl_button1',
        };

    pageManager.push(home) 
        .push(button) 
        .push(button1)
        .default1('home')
        .init();
});